package com.niji.littly_donkey_android.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
import com.android.volley.*;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.niji.littly_donkey_android.R;
import com.niji.littly_donkey_android.adapter.FeedbackAdapter;
import com.niji.littly_donkey_android.bean.Feedback;
import com.niji.littly_donkey_android.http.ConnectViaSession;
import com.niji.littly_donkey_android.util.JsonUtil;
import com.niji.littly_donkey_android.util.StatusBarUtil;
import com.niji.littly_donkey_android.util.UrlUtil;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.*;

public class BlackListActivity extends BaseActivity {
    private static final String TAG = "BlackListActivity";

    private Button black_list_return;

    private ListView black_list;



    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.blacklists_activity);
        init();
        blackListReturn();
    }


    @Override
    protected int getLayoutId() {
        return R.layout.blacklists_activity;

    }
    @Override
    protected int getStatusBarColor() {
        return R.color.white_easy_photos;
    }

    private void init() {
        black_list = findViewById(R.id.black_list);
        ArrayList<Feedback> feedbacks = new ArrayList<>();
        String url = UrlUtil.URL + "/user/black/list?page=1&per_page=100";
        RequestQueue requestQueue= Volley.newRequestQueue(BlackListActivity.this);
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                try {
                    Map<String, Object> map = JsonUtil.jsonToMap(s);
                    Integer code = (Integer) map.get("code");
                    if (code == 0) {
                        Map<String,Object> dataMap = (Map) map.get("data");
                        Log.i(TAG,dataMap.toString());
                        List<Map<String, Object>> mapList = (List<Map<String, Object>>) dataMap.get("data");
                        Log.i(TAG,mapList.toString());
                        for (int i = 0; i < mapList.size(); i++) {
                            Feedback feedback = new Feedback();
                            Integer id = (Integer) mapList.get(i).get("id");
                            feedback.setId(id);
                            String nickname = (String) mapList.get(i).get("nickname");
                            feedback.setName(nickname);
                            String avatar = (String) mapList.get(i).get("avatar");
                            feedback.setHead(UrlUtil.URL + avatar);
                            String signature = (String) mapList.get(i).get("signature");
                            feedback.setMessage(signature);
                            feedback.setBtn("解除拉黑");
                            feedbacks.add(feedback);
                        }

                    } else if(code == -1){
                        Toast.makeText(BlackListActivity.this, "登录失效，请重新登录", Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(BlackListActivity.this, GetPhoneCodeActivity.class);
                        intent.putExtra("loginCode","-1");
                        startActivity(intent);
                        finish();
                    }else {
                        Toast.makeText(getApplicationContext(), "黑名单获取失败", Toast.LENGTH_SHORT).show();
                        overridePendingTransition(R.anim.xpage_slide_in_left, R.anim.xpage_slide_out_right);
                        finish();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                black_list.setAdapter(new FeedbackAdapter(BlackListActivity.this,feedbacks));
                RelativeLayout black_list_loading = findViewById(R.id.black_list_loading);
                RelativeLayout black_list_main = findViewById(R.id.black_list_main);
                black_list_main.setVisibility(View.VISIBLE);
                black_list_loading.setVisibility(View.INVISIBLE);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e(TAG, volleyError.getMessage(), volleyError);
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                String sesseionid = new ConnectViaSession(BlackListActivity.this).GetSession();
                if (!sesseionid.equals("")) {
                    HashMap<String, String> headers = new HashMap<String, String>();
                    headers.put("cookie", sesseionid);
                    return headers;
                } else {
                    return super.getHeaders();
                }
            }
        };

        requestQueue.add(stringRequest);
    }

    /**
     * 返回
     */
    private void blackListReturn() {
        black_list_return = findViewById(R.id.black_list_return);
        black_list_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                overridePendingTransition(R.anim.xpage_slide_in_left,R.anim.xpage_slide_out_right);
                finish();
            }
        });

    }
}
